﻿#pragma execution_character_set("utf-8")

#include "page_video.h"
#include <QVariant>
#include "qlayout.h"
#include "qsplitter.h"
#include "qpushbutton.h"
#include "qstackedwidget.h"
#include <QDebug>
#include <QResizeEvent>

videoPage::videoPage(QWidget *parent) : QWidget(parent),m_bReal(true)
{
    this->initForm();
}

videoPage::~videoPage()
{
}

void videoPage::initForm()
{
    QHBoxLayout * horizontalLayout_1 = new QHBoxLayout(this);
    horizontalLayout_1->setSpacing(2);
    horizontalLayout_1->setContentsMargins(1, 1, 1, 1);

    QSplitter *splitter= new QSplitter(this);
    splitter->setFrameShape(QFrame::NoFrame);
    splitter->setFrameShadow(QFrame::Plain);
    splitter->setLineWidth(2);
    splitter->setOrientation(Qt::Horizontal);
    splitter->setOpaqueResize(true);
    splitter->setChildrenCollapsible(false);
    
    m_devlistPage = new deviceListPage(splitter);
    QSizePolicy sizePolicy1(QSizePolicy::Minimum, QSizePolicy::Minimum);
    m_devlistPage->setSizePolicy(sizePolicy1);
    m_devlistPage->setMinimumSize(QSize(270, 350));
    m_devlistPage->setVisible(true);
    splitter->addWidget(m_devlistPage);

    m_stackedWidget = new QStackedWidget(splitter);
    m_stackedWidget->setSizePolicy(sizePolicy1);
    m_stackedWidget->setMinimumSize(QSize(350, 350));    

    m_realtimePage = new realtimePage();
    m_stackedWidget->addWidget(m_realtimePage);
    m_playbackPage = new playbackPage();
    m_stackedWidget->addWidget(m_playbackPage);
    m_stackedWidget->setCurrentIndex(0);

    m_letfHideBtn = new QPushButton(this);
    m_letfHideBtn->setProperty("letf_hide", 0);
    m_letfHideBtn->setFixedSize(15, 30);
    QSizePolicy sizePolicy2(QSizePolicy::Fixed, QSizePolicy::Fixed);
    m_letfHideBtn->setSizePolicy(sizePolicy2);
    m_rightHideBtn = new QPushButton(this);
    m_rightHideBtn->setProperty("rigth_hide", 0);
    m_rightHideBtn->setFixedSize(15, 30);
    m_rightHideBtn->setSizePolicy(sizePolicy2);

    connect(m_letfHideBtn, SIGNAL(clicked()), this, SLOT(on_letfHide_click()));
    connect(m_rightHideBtn, SIGNAL(clicked()), this, SLOT(on_rightHide_click()));
    connect(m_devlistPage, SIGNAL(resize_x()), this, SLOT(btn_move()));

    splitter->addWidget(m_stackedWidget);

    splitter->setStretchFactor(0,0);
    splitter->setStretchFactor(1,1);

    horizontalLayout_1->addWidget(splitter);
}

void videoPage::setRealtime(bool realtime)
{
    m_bReal = realtime;
    m_stackedWidget->setCurrentIndex(realtime?0:1);
}

void videoPage::on_letfHide_click()
{
    if (m_devlistPage->isVisible())
    {
        m_devlistPage->setVisible(false);
        m_letfHideBtn->setProperty("letf_hide", 1);
    }
    else
    {
        m_devlistPage->setVisible(true);
        m_letfHideBtn->setProperty("letf_hide", 0);
    }

    btn_move();
}

void videoPage::on_rightHide_click()
{
    int right_hide = m_rightHideBtn->property("rigth_hide").toInt();
    if (right_hide == 0)
    {
        m_rightHideBtn->setProperty("rigth_hide", 1);
    }
    else
    {
        m_rightHideBtn->setProperty("rigth_hide", 0);
    }
    if (m_bReal)
    {
        m_realtimePage->hideRight(right_hide == 0 ? false:true);
    }

    btn_move();
}

void videoPage::resizeEvent(QResizeEvent *ev)
{
    if (ev)
    {
        QWidget::resizeEvent(ev);
        if (ev->oldSize() != ev->size())
        {
            btn_move();
        }
    }
    else
    {
        btn_move();
    }
}

void videoPage::btn_move()
{
    int h = this->height();
    int w = this->width();
    int x1 = 1;
    int left_hide = m_letfHideBtn->property("letf_hide").toInt();
    if (left_hide == 0)
    {
       x1 = m_devlistPage->width()-5;
    }

    int x2 = w - 16;
    int right_hide = m_rightHideBtn->property("rigth_hide").toInt();
    if (right_hide == 0)
    {
       x2 = w - 275;
    }

    m_letfHideBtn->move(x1, h/2-15);
    m_rightHideBtn->move(x2, h/2-15);
}

void videoPage::fullScreen(bool full)
{
    if(full)
    {
        m_devlistPage->setVisible(false);
        m_letfHideBtn->setVisible(false);
        m_rightHideBtn->setVisible(false);
    }
    else
    {
        m_letfHideBtn->setVisible(true);
        m_rightHideBtn->setVisible(true);
        m_devlistPage->setVisible(true);
    }
    if (m_bReal)
    {
        m_realtimePage->fullScreen(full);
    }
}
